package cn.kgc.vue.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Template;
import cn.kgc.vue.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/7/11
 * @description
 */
@RestController
@RequestMapping("template")
@Api(tags = "资源中心")
public class TemplateController {


    @Resource
    private TemplateService templateService;

    @ApiOperation("资源列表")
    @PostMapping("page")
    public ResponseResult templatePage(@RequestBody QueryCondition<Template> queryCondition){

        return templateService.templatePage(queryCondition);
    }


    @ApiOperation("资源上传")
    @PostMapping("upload")
    public ResponseResult uploadTemplate(MultipartFile file){

        if (file.isEmpty()){
            return  ResponseResult.fail().message("请选择文件");
        }

        String originalFilename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
        String uuid = IdUtil.simpleUUID();
        String uuidName = uuid+"."+filenameExtension;
        Double size = file.getSize()/1024.0;

        String formatSize = String.format("%.2f", size);


        File f = new File("D:/upload/springboot/"+uuidName);

        try {
            file.transferTo(f);
            return  ResponseResult.success().data("uuidName",uuidName).data("size",formatSize) ;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @ApiOperation("添加资源信息")
    @PostMapping("add")
    public ResponseResult addTemplateInfo(@RequestBody Template template){

        templateService.save(template);
        return  ResponseResult.success().message("资源信息添加成功");
    }

    @ApiOperation("删除资源信息")
    @DeleteMapping("delete/{id}")
    public ResponseResult deleteTemplateInfo(@PathVariable("id") String uuid){


        return  templateService.deleteTemplate(uuid);
    }


    @ApiOperation("下载资源")
    @PostMapping("download")
    public void downloadTemplate(@RequestBody Map<String,String> params, HttpServletResponse response) throws IOException {

        String uuid = params.get("id");
        File file = new File("D:/upload/springboot/" + uuid);
        FileInputStream fileInputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.copy(fileInputStream,outputStream);
        outputStream.close();
        fileInputStream.close();

    }
}
